package com.kingyea.mobilepolice.config.feign;

import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;

/**
 * Feign
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/3/29 21:31
 */
@Component
@Configuration
public class FeignCfg {

//    @Bean
//    Retryer feignRetryer() {
//        return new Retryer.Default();
//    }

    @Bean
    public RequestInterceptor headerInterceptor() {
        return requestTemplate -> {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                String access_token = request.getParameter("access_token");// url 形式传递token
                if (request != null) {
                    Enumeration<String> headerNames = request.getHeaderNames();
                    if (headerNames != null) {
                        while (headerNames.hasMoreElements()) {
                            String name = headerNames.nextElement();
                            String values = request.getHeader(name);
                            requestTemplate.header(name, values);// todo 注意：这里将所有的header都传递下去
                            if (name.equals("Authorization") && values.startsWith("Bearer ")) {// 通过header传递的token
                                // 但是如果header显式写了token字段，则以显式token为准
                                if (!requestTemplate.headers().containsKey("token")) {
                                    requestTemplate.header("token", values);
                                }
                            }
                        }
                    }
                    if (!requestTemplate.headers().containsKey("token")) {// 如果header里边不包含 token、Authorization，那么就从access_token里边拿
                        requestTemplate.header("token", access_token);
                    }
                }
            }
            Map<String, Collection<String>> headers = requestTemplate.headers();
            if (StringUtils.isEmpty(headers.get("Authorization"))) {// 如果header里边没有，就去body里边拿？该逻辑好像可以去除
                byte[] body = requestTemplate.body();
                if (body != null && body.length > 0) {
                    Map<String, Object> map = JSON.parseObject(body, Map.class);
                    Object tokenObj = map.get("Authorization");
                    if (!StringUtils.isEmpty(tokenObj)) {
                        Collection<String> tokenElem = new ArrayList<>();
                        if (tokenObj.getClass().getName().equals(String.class.getName())) {
                            tokenElem.add((String) tokenObj);
                            requestTemplate.header("Authorization", tokenElem);
                        }
                    }
                }
            }
        };
    }

}
